This module introduces creating dashboard of data visualization to publish on website.
Plotly is a suite of tools using JavaScript to render interactive graphics. It is available in R and can generate interactive charts for publication on website.
# install.packages("plotly")
# load the plotly R package
require(plotly)
library(plotly)
require("readr")
library(readr)
library(tidyverse)
# load the diamonds dataset from the ggplot2 package
data(diamonds, package = "ggplot2")
# create three visualizations of the diamonds dataset
plot_ly(diamonds, x = ~cut)
plot_ly(diamonds, x = ~cut, y = ~clarity)
plot_ly(diamonds, x = ~cut, color = ~clarity, colors = "Accent")
hkid <- read_csv("https://raw.githubusercontent.com/kho777/data-visualization/master/data/hkid.csv")
CO2 <- read_csv("http://raw.githubusercontent.com/kho777/data-visualization/master/data/CO2.csv")
hpi2016 <- read_csv("http://raw.githubusercontent.com/kho777/data-visualization/master/data/hpi2016.csv")
require(dplyr)
library(plyr)
rename(hpi2016, c("GDPC"="GDPPC"))
## # A tibble: 140 × 11
## Region AverageLifeExpect… AverageWellbeing HappyLifeYears Footprint
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 Middle East and… 59.7 3.8 12.4 0.8
## 2 Post-communist 77.3 5.5 34.4 2.2
## 3 Middle East and… 74.3 5.6 30.5 2.1
## 4 Americas 75.9 6.5 40.2 3.1
## 5 Post-communist 74.4 4.3 24 2.2
## 6 Asia Pacific 82.1 7.2 53.1 9.3
## 7 Europe 81 7.4 54.4 6.1
## 8 Asia Pacific 70.8 4.7 23.3 0.7
## 9 Post-communist 70.9 5.7 34 5.1
## 10 Europe 80.4 6.9 49.5 7.4
## # … with 130 more rows, and 6 more variables: InequalityofOutcomes <chr>,
## # Inequality-adjustedLifeExpectancy <dbl>,
## # Inequality-adjustedWellbeing <dbl>, HappyPlanetIndex <dbl>, GDPPC <chr>,
## # Population <dbl>
plot_ly(hkid, x = ~Hongkonger)
plot_ly(hpi2016, x = ~GDPPC, y = ~HappyPlanetIndex)
# plot_ly(hpi2016, x = ~GDPPC, y = ~HappyPlanetIndex, z= ~hpi2016$AverageLifeExpectancy, type = "surface")
add_lines(plot_ly(hpi2016, x = ~GDPPC, y = ~HappyPlanetIndex))
plot_ly(CO2, x = ~CO2pc)
plot_ly(CO2, z = ~CO2pc)
plot_ly(diamonds, x = ~cut, color = ~clarity, colors = "Accent")
# add_bars(), add_lines(), add_heatmap(), add_boxplot()
hpi2016 %>% plot_ly(x = ~GDPPC, y = ~HappyPlanetIndex) %>% add_bars()
hpi2016 %>% plot_ly(x = ~GDPPC, y = ~HappyPlanetIndex, z = ~AverageLifeExpectancy) %>% add_heatmap(inherit = TRUE)
# hpi2016 %>% plot_ly(x = ~GDPPC, y = ~HappyPlanetIndex, z = ~HappyLifeYears) %>% add_histogram2dcontour()
hpi2016 %>% plot_ly(x = ~Region, y = ~HappyPlanetIndex) %>% add_boxplot()
Shiny is a RStudio suite for creating Reactive and Interactive data visualization applications to be published on websites.
## Introduction to R Shiny sample program 2
## file: IntroShiny02.R
## Adapted from Beeley, Chris and Shitalkumar R. Sukhdeve, 2018. Web application development
## with R using Shiny (3rd ed.) Packt Publishing Ltd.
## Also, https://shiny.rstudio.com
# Packages: readr, shiny
# This example demonstrates a core feature of Shiny: **reactivity**.
# In the `server` function, a reactive called `datasetInput` is declared.
# Notice that the reactive expression depends on the input expression `input$dataset`,
# and that it's used by two output expressions: `output$summary` and `output$view`.
# Try changing the dataset (using *Choose a dataset*) while looking at the reactive and then at the outputs;
# you will see first the reactive and then its dependencies flash.
# Notice also that the reactive expression doesn't just update whenever anything changes--only the inputs it depends on will trigger an update.
# Change the "Caption" field and notice how only the `output$caption` expression is re-evaluated; the reactive and its dependents are left alone.
require("readr")
require("shiny")
# Loading datasets
hkid <- read_csv("https://raw.githubusercontent.com/kho777/data-visualization/master/data/hkid.csv")
CO2 <- read_csv("http://raw.githubusercontent.com/kho777/data-visualization/master/data/CO2.csv")
hpi2016 <- read_csv("http://raw.githubusercontent.com/kho777/data-visualization/master/data/hpi2016.csv")
# Define UI for dataset viewer app ----
ui <- fluidPage(
# App title ----
titlePanel("Karl Ho Datasets"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Text for providing a caption ----
# Note: Changes made to the caption in the textInput control
# are updated in the output area immediately as you type
textInput(inputId = "caption",
label = "Caption:",
value = "Datasets"),
# Input: Selector for choosing dataset ----
selectInput(inputId = "dataset",
label = "Choose a dataset:",
choices = c("Hong Kong Identity", "HPI 2016","CO2 emissions")),
# Input: Numeric entry for number of obs to view ----
numericInput(inputId = "obs",
label = "Number of observations to view:",
min=0,
value = 10)
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Formatted text for caption ----
h3(textOutput("caption", container = span)),
# Output: Verbatim text for data summary ----
verbatimTextOutput("summary"),
# Output: HTML table with requested number of observations ----
tableOutput("view")
)
)
)
# Define server logic to summarize and view selected dataset ----
server <- function(input, output) {
# Return the requested dataset ----
# By declaring datasetInput as a reactive expression we ensure
# that:
#
# 1. It is only called when the inputs it depends on changes
# 2. The computation and result are shared by all the callers,
# i.e. it only executes a single time
datasetInput <- reactive({
switch(input$dataset,
"Hong Kong Identity" =hkid,
"HPI 2016"=hpi2016,
"CO2 emissions"=CO2)
})
# Create caption ----
# The output$caption is computed based on a reactive expression
# that returns input$caption. When the user changes the
# "caption" field:
#
# 1. This function is automatically called to recompute the output
# 2. New caption is pushed back to the browser for re-display
#
# Note that because the data-oriented reactive expressions
# below don't depend on input$caption, those expressions are
# NOT called when input$caption changes
output$caption <- renderText({
input$caption
})
# Generate a summary of the dataset ----
# The output$summary depends on the datasetInput reactive
# expression, so will be re-executed whenever datasetInput is
# invalidated, i.e. whenever the input$dataset changes
output$summary <- renderPrint({
dataset <- datasetInput()
summary(dataset)
})
# Show the first "n" observations ----
# The output$view depends on both the databaseInput reactive
# expression and input$obs, so it will be re-executed whenever
# input$dataset or input$obs is changed
output$view <- renderTable({
head(datasetInput(), n = input$obs)
})
}
# Create Shiny app ----
shinyApp(ui, server)